/*
		ServEngine framework library
		A library to fast develop JEE online applications
	
		Copyright 2008 José Ignacio de Córdoba Álvaro

		Licensed under the Apache License, Version 2.0 (the "License");
		you may not use this file except in compliance with the License.
		You may obtain a copy of the License at

				http://www.apache.org/licenses/LICENSE-2.0

		Unless required by applicable law or agreed to in writing, software
		distributed under the License is distributed on an "AS IS" BASIS,
		WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
		See the License for the specific language governing permissions and
		limitations under the License.
	
		Jose Ignacio de Cordoba Alvaro
		http://ignacio.decordoba.com
*/

package com.servengine.image;

import java.awt.*;
import java.awt.image.MemoryImageSource;
import java.io.File;
import java.io.FileInputStream;


class BMPUtils
{

    BMPUtils()
    {
    }

    static Dimension getDim(File file)
    {
        int i = 0;
        int j = 0;
        try
        {
            FileInputStream fileinputstream = new FileInputStream(file);
            fileinputstream.skip(14L);
            byte abyte0[] = new byte[40];
            fileinputstream.read(abyte0, 0, 40);
            i = dwordToInt(abyte0, 4);
            j = dwordToInt(abyte0, 8);
            fileinputstream.close();
        }
        catch(Exception exception)
        {
            return null;
        }
        return new Dimension(i, j);
    }

    static Image load(File file)
    {
        Image image;
        try
        {
            FileInputStream fileinputstream = new FileInputStream(file);
            fileinputstream.skip(14L);
            byte abyte0[] = new byte[40];
            fileinputstream.read(abyte0, 0, 40);
            int i = dwordToInt(abyte0, 4);
            int j = dwordToInt(abyte0, 8);
            int k = wordToInt(abyte0, 14);
            int l = dwordToInt(abyte0, 20);
            int i1 = dwordToInt(abyte0, 32);
            int j1 = dwordToInt(abyte0, 16);
            if(j1 != 0)
                return null;
            if(k == 24)
            {
                int ai[] = new int[j * i];
                int l1 = 4 - (i * 3) % 4;
                if(l1 == 4)
                    l1 = 0;
                int j2 = j * (i * 3 + l1);
                byte abyte1[] = new byte[j2];
                fileinputstream.read(abyte1, 0, j2);
                int l2 = 0;
                for(int i3 = 0; i3 < j; i3++)
                {
                    for(int k3 = 0; k3 < i; k3++)
                    {
                        ai[i * (j - i3 - 1) + k3] = 0xff000000 | (abyte1[l2 + 2] & 0xff) << 16 | (abyte1[l2 + 1] & 0xff) << 8 | abyte1[l2] & 0xff;
                        l2 += 3;
                    }

                    l2 += l1;
                }

                image = Toolkit.getDefaultToolkit().createImage(new MemoryImageSource(i, j, ai, 0, i));
            } else
            if(k == 8)
            {
                int k1 = 0;
                if(i1 > 0)
                    k1 = i1;
                else
                    k1 = 1 << k;
                int i2 = 4 - i % 4;
                if(i2 == 4)
                    i2 = 0;
                int k2 = j * (i + i2);
                int ai1[] = new int[k1];
                byte abyte2[] = new byte[k1 * 4];
                fileinputstream.read(abyte2, 0, k1 * 4);
                int j3 = 0;
                for(int l3 = 0; l3 < k1; l3++)
                {
                    ai1[l3] = 0xff000000 | (abyte2[j3 + 2] & 0xff) << 16 | (abyte2[j3 + 1] & 0xff) << 8 | abyte2[j3] & 0xff;
                    j3 += 4;
                }

                int ai2[] = new int[i * j];
                byte abyte3[] = new byte[k2];
                fileinputstream.read(abyte3, 0, k2);
                j3 = 0;
                for(int i4 = 0; i4 < j; i4++)
                {
                    for(int j4 = 0; j4 < i; j4++)
                    {
                        ai2[i * (j - i4 - 1) + j4] = ai1[abyte3[j3] & 0xff];
                        j3++;
                    }

                    j3 += i2;
                }

                image = Toolkit.getDefaultToolkit().createImage(new MemoryImageSource(i, j, ai2, 0, i));
            } else
            {
                return null;
            }
            fileinputstream.close();
        }
        catch(Exception exception)
        {
            return null;
        }
        return image;
    }

    static int wordToInt(byte abyte0[], int i)
    {
        return (abyte0[i + 1] & 0xff) << 8 | abyte0[i] & 0xff;
    }

    static int dwordToInt(byte abyte0[], int i)
    {
        return (abyte0[i + 3] & 0xff) << 24 | (abyte0[i + 2] & 0xff) << 16 | (abyte0[i + 1] & 0xff) << 8 | abyte0[i] & 0xff;
    }

    static byte[] intToWord(int i)
    {
        if(i == 0)
        {
            buf2[0] = 0;
            buf2[1] = 0;
        } else
        {
            buf2[0] = (byte)(i & 0xff);
            buf2[1] = (byte)(i >> 8 & 0xff);
        }
        return buf2;
    }

    static byte[] intToDWord(int i)
    {
        if(i == 0)
        {
            buf4[0] = 0;
            buf4[1] = 0;
            buf4[2] = 0;
            buf4[3] = 0;
        } else
        {
            buf4[0] = (byte)(i & 0xff);
            buf4[1] = (byte)(i >> 8 & 0xff);
            buf4[2] = (byte)(i >> 16 & 0xff);
            buf4[3] = (byte)(i >> 24 & 0xff);
        }
        return buf4;
    }

    static final int bilen = 40;
    static byte buf2[] = new byte[2];
    static byte buf4[] = new byte[4];

}
